package icu.wangrui.bookstore.controller;

import icu.wangrui.bookstore.pojo.User;
import icu.wangrui.bookstore.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
@Validated  //数据校验
public class UserLoginController {
    @Autowired
    private UserServiceImpl userService;

    @RequestMapping("/login")
    public String login(
            @RequestParam("uname") String username,
            @RequestParam("upwd") String password,
            Model model, HttpSession session) {
        //具体业务

        if (!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) {
            Boolean user1 = userService.checkUserByName(username);
            if (!user1){
                //告诉用户登录失败
                model.addAttribute("msg", "用户不存在");
                return "user/page/login";
            }
            User user = userService.queryUserByName(username, password);
            if (user != null) {
                session.setAttribute("loginUser", user);
                return "redirect:page/index";
            }else {
                //告诉用户登录失败
                model.addAttribute("msg", "密码错误");
                model.addAttribute("username", username);
                return "user/page/login";
            }
        }
        //告诉用户登录失败
        model.addAttribute("msg", "用户名或密码错误");
        return "user/page/login";

    }

    @RequestMapping("/regist")
    public String register(
           User user,
           Model model, HttpSession session) {
        //具体业务
        if (StringUtils.isEmpty(user.getUname())){
            return "";
        }
        if (StringUtils.isEmpty(user.getUpwd())){
            return "";
        }
        if (StringUtils.isEmpty(user.getEmail())){
            return "";
        }
        if (StringUtils.isEmpty(user.getPhone())){
            return "";
        }
        Boolean userRegister = userService.userRegister(user);
        if (userRegister){
            model.addAttribute("msg", "注册成功");
            //这里可以将数据保存，在登录页面回显
            return "redirect:/userlogin.html";
        }

        //告诉用户注册失败
        model.addAttribute("msg", "请输入完整信息");
        return "/user-regist.html";

    }

    @RequestMapping("/unameCheck")
    @ResponseBody
    public Boolean unameCheck(String uname){
       Boolean unameCheck = userService.checkUserByName(uname);
        return unameCheck;
    }

    @RequestMapping("/uphoneCheck")
    @ResponseBody
    public Boolean uphoneCheck(String phone){
       Boolean uphoneCheck = userService.checkUserByPhone(phone);
        return uphoneCheck;
    }



    @GetMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("loginUser");
        /*
        * session.invalidate()是将session设置为失效，一般在退出时使用，
        * 但要注意的是：session失效的同时 浏览器会立即创建一个新的session的，
        * 你第一个session已经失效了 所以调用它的getAttribute方法时候一定会抛出NullPointerException的
        * session.invalidate();
        * */
        return "redirect:/userlogin.html";
    }

}
